package com.jjwxc.jwjskandriod.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.ReportResponse;
import com.jjwxc.jwjskandriod.readActivity.utils.UMConstant;
import com.umeng.analytics.MobclickAgent;

public class RegisterActivity extends BaseActivity {
    private TextView tv_next;
    private TextView tv_code;

    private EditText ed_phone;
    private EditText ed_code;

    private CheckBox checkbox;
    private TextView tv_reg_one;
    private TextView tv_reg_two;
    private LinearLayout ll_fanhui;


    @Override
    public int getContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    public void findView() {
        tv_next = findViewById(R.id.tv_next);
        ll_fanhui = findViewById(R.id.ll_fanhui);

        ed_phone = findViewById(R.id.ed_phone);
        ed_code = findViewById(R.id.ed_code);
        tv_code = findViewById(R.id.tv_code);
        checkbox = findViewById(R.id.checkbox);
        tv_reg_one = findViewById(R.id.tv_reg_one);
        tv_reg_two = findViewById(R.id.tv_reg_two);
        checkbox.setChecked(false);

    }

    @Override
    public void setListener() {
        ll_fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_next.setOnClickListener(v -> {
            String phone = ed_phone.getText().toString();
            String code = ed_code.getText().toString();

            if (StUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }

            if (StUtils.isEmpty(code)) {
                showToast("请输入验证码");
                return;
            }
            if (!checkbox.isChecked()) {
                showToast("请阅读并同意协议");
                return;
            }
            //验证验证码
            verifyCode(phone, code);
        });

        tv_code.setOnClickListener(v -> {
            String phone = ed_phone.getText().toString();
            if (StUtils.isEmpty(phone)) {
                showToast("请输入手机号");
                return;
            }
            sendSms(phone);
        });
        tv_reg_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, WebActivity.class);
                intent.putExtra("WEB_URL", Url.registerUser);
                intent.putExtra("WEB_NAME", "用户注册协议");
                startActivity(intent);
            }
        });
        tv_reg_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, WebActivity.class);
                intent.putExtra("WEB_URL", Url.privacy);
                intent.putExtra("WEB_NAME", "用户隐私条款");
                startActivity(intent);
            }
        });
    }

    @Override
    public void afterCreate() {
        setNoTitle();
        String phone = getIntent().getStringExtra("phone");
        ed_phone.setText(phone);
    }


    public void sendSms(String phone) {
        post(Url.sendSms, null, String.class, new FFNetWorkCallBack<String>() {
            @Override
            public void onSuccess(String response) {
                if (response.contains("200")) {
                    tv_code.setEnabled(false);
                    timer.start();
                    MobclickAgent.onEvent(RegisterActivity.this, UMConstant.SendVerifyCode);
                } else if (response.contains("328")) {
                    showToast("用户账号已存在");
                }

            }

            @Override
            public boolean onFail(String response) {
                showToast("请检查网络");
                return false;
            }
        }, "{\"mobile\":\"" + phone + "\",\"reason\":\"Register\"}");
    }

    public void verifyCode(String mobile, String code) {
        post(Url.verifyCode, "", ReportResponse.class, new FFNetWorkCallBack<ReportResponse>() {
            @Override
            public void onSuccess(ReportResponse response) {
                if (response.getCode() == 200) {
                    Intent intent = new Intent(RegisterActivity.this, PasswordActivity.class);
                    intent.putExtra("nextCode", response.getData());
                    intent.putExtra("phone", mobile);
                    startActivity(intent);
                } else if (response.getCode() == 328) {
                    showToast("用户登录账号已存在");
                }
            }

            @Override
            public boolean onFail(ReportResponse response) {
                return false;
            }
        }, "{\"mobile\":\"" + mobile + "\",\"code\":\"" + code + "\",\"reason\":\"Register\"}");
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tv_code.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            tv_code.setEnabled(true);
            tv_code.setText("发送验证码");
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }


}

